/*
 * @Description: 
 * @Author: YeKe
 * @Date: 2022-10-15 19:29:08
 * @LastEditors: YeKe
 * @LastEditTime: 2022-10-28 15:27:02
 */
import * as Cookies from 'js-cookie'
import { delStringBlankAll, isJSON } from "@/utils/operate"

export const state = () => ({
    issueStatus: '0', // 发布招标的类型
    passwordSchedule: { // 设置密码进度
        status: '0', // 邮箱/手机验证码
        name: 'SelectType', // 切换组件的名称
        active: 0, // 切换组件的进度
        formData: {} // 提交的表单
    },
})
export const mutations = {
    set_breadcrumb: (state, arr) => {
        state.breadcrumb = arr
    },
    set_issueStatus: (state, status) => {
        state.issueStatus = status
    },
    set_passwordSchedule: (state, obj) => {
        state.passwordSchedule = obj
    },
}
export const actions = {
    nuxtServerInit({ commit, state }, { req, $cookies }) {
        if (req.headers.cookie) {
            const cookieKeyArr = req.headers.cookie.split(';')
            cookieKeyArr.forEach(item => {
                const arr = item.split('=')
                const stateKey = delStringBlankAll(arr[0])
                const stateValue = $cookies.get(stateKey)
                if (Object.prototype.hasOwnProperty.call(state, stateKey)) {
                    if (isJSON(stateValue)) {
                        commit(`set_${stateKey}`, JSON.parse(stateValue))
                    } else {
                        commit(`set_${stateKey}`, stateValue)
                    }

                }
            });
        }

    },
    setIssueStatus({ commit }, status) {
        Cookies.set('issueStatus', status)
        commit("set_issueStatus", status)
    },
    setPasswordSchedule({ commit }, obj) {
        Cookies.set('passwordSchedule', JSON.stringify(obj))
        commit("set_passwordSchedule", obj)
    },

}
